package com.qf.controller;

import com.qf.pojo.Shop;
import com.qf.service.ShopService;
import com.sun.org.apache.bcel.internal.classfile.Signature;
import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpRequest;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;

import java.util.ArrayList;
import java.util.List;

/*
@author fu
@date 2022/3/2622:20
商品的控制器
*/
@Controller
@RequestMapping("/shop")
public class ShopController {

    @Autowired
    //注入Service
    private ShopService shopService;

    //查询所有商品
    @RequestMapping("/findAll")
    public String findAllShop(Model model){

        List<Shop> list = shopService.getAllShop();
        //将list存储在Model中
        model.addAttribute("list",list);
        System.out.println("执行商品查询任务");
        return "shop_list";
    }

    //点击修改时,跳转到shop界面
    @RequestMapping("/find")
    public String findShopById(Integer shopId,Model model){

        Shop shop = shopService.getShop(shopId);
        model.addAttribute("shop",shop);
        return "shop";
    }

    //修改商品信息
    @RequestMapping("/update")
    public String updateShop(Shop shop){

        shopService.updateShop(shop);
        System.out.println("执行商品修改任务");
        return "redirect:findAll";
    }

    //删除商品
    @RequestMapping("/delte")
    public String deleteShop(Integer shopId){

        System.out.println(shopId);
        shopService.deteleShop(shopId);
        System.out.println("执行删除任务");
        return "redirect:/shop/findAll";
    }

    //点击添加时,跳转到添加页面
    @RequestMapping("/toadd")
    public String toAddShop(){

        return "addshop";
    }

    //添加商品
    @RequestMapping("/add")
    public String addShop(Shop shop){

        shopService.addShop(shop);
        System.out.println("执行添加任务");
        return "redirect:/shop/findAll";
    }
}
